package com.cxrj.zhsq.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.alibaba.fastjson.JSON;
import com.cxrj.zhsq.base.entity.FiveElements;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QrCodeUtils {

	public static final int WIDTH = 300;
	public static final int HEIGHT = 300;
	public static final String FORMAT = "png";
	public static final String CHARTSET = "utf-8";

	/**
	 * 
	 * @Title:createQRcode
	 * @Description:创建二维码
	 * @param filePath
	 * @author doubledumbao
	 * @修改时间：2018年2月26日 上午9:44:45
	 * @修改内容：创建
	 */
	public static void createQRcode(String filePath, String text, FiveElements fiveElements) {
		/**
		 * 如果用的jdk是1.9，需要配置下面这一行。
		 */
		// System.setProperty("java.specification.version", "1.9");
		// FiveElements fiveElements = new FiveElements();
		// fiveElements.setName("zhao");
		// fiveElements.setGender("M");
		// fiveElements.setIdType("I");
		// fiveElements.setIdno("370983");
		// fiveElements.setMobile("1805310");
		String contents = JSON.toJSONString(fiveElements);
		HashMap<EncodeHintType, Object> hints = new HashMap<>();
		hints.put(EncodeHintType.CHARACTER_SET, CHARTSET);
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		hints.put(EncodeHintType.MARGIN, 2);
		try {
			BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
			Path file = new File(filePath).toPath();
			MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, file);
			String str = "1号门";
			int startX = (WIDTH - (18 * str.length())) / 2;
			int startY = (HEIGHT - 10) - ((HEIGHT - 10) - WIDTH) / 2;
			File file1 = new File(filePath);
			Image src = ImageIO.read(file1);
			int imageW = src.getWidth(null);
			int imageH = src.getHeight(null);
			BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB);
			Graphics g = image.createGraphics();
			g.drawImage(src, 0, 0, imageW, imageH, null);
			g.setColor(Color.BLACK);
			g.setFont(new Font(null, 1, 18));
			g.drawString(str, startX, startY);
			g.dispose();

			// FileOutputStream out = new FileOutputStream(filePath);
			// ImageIO.write(image, FORMAT, out);
			// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			// encoder.encode(image);
			// out.close();
			String formatName = filePath.substring(filePath.lastIndexOf(".") + 1);
			ImageIO.write(image, formatName, new File(filePath));

			System.out.println("创建二维码完成");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @Title:getQRresult
	 * @Description:读取二维码
	 * @param filePath
	 * @return
	 * @author doubledumbao
	 * @修改时间：2018年2月26日 上午9:45:19
	 * @修改内容：创建
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Result getQRresult(String filePath) {
		/**
		 * 如果用的jdk是1.9，需要配置下面这一行。
		 */
		// System.setProperty("java.specification.version", "1.9");
		Result result = null;
		try {
			File file = new File(filePath);

			BufferedImage bufferedImage = ImageIO.read(file);
			BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));

			HashMap hints = new HashMap<>();
			hints.put(EncodeHintType.CHARACTER_SET, CHARTSET);
			result = new MultiFormatReader().decode(bitmap, hints);
		} catch (NotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}

	public static void main(String[] args) {
		String filePath = "D:/zhao.png";
		FiveElements fiveElements = new FiveElements();
		fiveElements.setName("zhao");
		fiveElements.setGender("M");
		fiveElements.setIdType("I");
		fiveElements.setIdno("370983");
		fiveElements.setMobile("1805310");
		createQRcode(filePath, "1号门", fiveElements);
		Result result = getQRresult(filePath);
		if (result != null) {
			System.out.println("二维码内容：" + result.getText());
			System.out.println("二维码格式：" + result.getBarcodeFormat());
		}
	}
}
